//
//  JXNavigationController.m
//  JiaoX
//
//  Created by jiaoxiu－004 on 14-10-11.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXNavigationController.h"
#import "UserDefaultsHelper.h"
#import "JXAlertView.h"
#import "JXLoginRegistCheckController.h"

@interface JXNavigationController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
@end

@implementation JXNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    __weak JXNavigationController *weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
        self.delegate = weakSelf;
    }
}



+(void)initialize{
    
    //设置导航栏的顶部文字颜色与大小
    
    UINavigationBar *appearance = [UINavigationBar appearance];
    
    // 设置文字属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    textAttrs[NSFontAttributeName] = JXNavigationTitleFont;
    // UIOffsetZero是结构体, 只要包装成NSValue对象, 才能放进字典\数组中
    textAttrs[UITextAttributeTextShadowOffset] = [NSValue valueWithUIOffset:UIOffsetZero];
    [appearance setTitleTextAttributes:textAttrs];

}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //如果在push过程中触发手势滑动返回，会导致导航栏崩溃（从日志中可以看出）。针对这个问题，我们需要在pop过程禁用手势滑动返回功能
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
        self.interactivePopGestureRecognizer.enabled = NO;
    if (self.viewControllers.count > 0) { // 如果现在push的不是栈底控制器(最先push进来的那个控制器)
        viewController.hidesBottomBarWhenPushed = YES;
        // 设置导航栏按钮
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_back" highImageName:@"navigationbar_back_highlighted" target:self action:@selector(back)];
    }
    if ([viewController respondsToSelector:@selector (shouldLogin)]) {
        if ([viewController performSelector:@selector(shouldLogin) withObject:viewController]) {
            NSString *token = [UserDefaultsHelper stringForKey:KEY_APPTOKEN];
            if([Utils isBlankString:token]){//token为空说明没有登录
                JXNavigationController *nav = [[JXNavigationController alloc]initWithRootViewController:[[JXLoginRegistCheckController alloc]init]];
                [self presentViewController:nav animated:YES completion:^{}];
                return;
            }
        }
    }
    [super pushViewController:viewController animated:animated];
}
- (void)back
{
    [self popViewControllerAnimated:YES];
}

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
        self.interactivePopGestureRecognizer.enabled = YES;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotiDeal:) name:KEY_NOTI_PUSH_STRING object:nil];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)pushNotiDeal:(NSNotification *)notification{
    NSDictionary *userInfo = notification.userInfo;
    [self dealNoti:userInfo isEnterForeground:[notification.object boolValue]];
}

-(void)dealNoti:(NSDictionary *)userInfo isEnterForeground:(BOOL)isEnterForeground{
    if (isEnterForeground) {
        NSNumber *messageType = [userInfo objectForKey:@"messageType"];
        if ([messageType isEqualToNumber:[NSNumber numberWithInt:2]]) {
            //推送的是url;
            NSString *url = [userInfo objectForKey:@"value"][0];
            [Utils dealUrlWithController:self.viewControllers.lastObject url:url title:@"" fromWebView:NO];
        }else{
            //推送的是其他东西(帖子被回复，评论被回复，评测被回复)
            [Utils dealNotiWithController:self.viewControllers.lastObject notiDic:userInfo isForgorund:isEnterForeground];
        }
    }else{
        NSNumber *messageType = [userInfo objectForKey:@"messageType"];
        if ([messageType isEqualToNumber:[NSNumber numberWithInt:2]]) {
            //推送的是url;
            
            NSString *title = userInfo[@"title"];
            NSString *content = userInfo[@"aps"][@"alert"];
            
            JXAlertView *alert = [[JXAlertView alloc]initWithTitle:title message:content delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"打开 ", nil];
            alert.obj = userInfo;
            [alert show];
        }else{
            //推送的是其他东西(帖子被回复，评论被回复，评测被回复)
            [Utils dealNotiWithController:self.viewControllers.lastObject notiDic:userInfo isForgorund:isEnterForeground];
        }
        JXLog(@"%@",userInfo);
    }
}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    //判断当前是否是第一个控制器，如果是的话则不手势识别
    if(self.viewControllers.count==1){
        return NO;
    }
    return YES;
}

#pragma mark - uialertview 代理
//-(void)willPresentAlertView:(UIAlertView *)alertView{
//    // 遍历 UIAlertView 所包含的所有控件
//    for (UIView *tempView in alertView.subviews) {
//        if ([tempView isKindOfClass:[UIButton class]]) {
//            // 当该控件为一个 UILabel 时
//            UIButton *tempLabel = (UIButton *) tempView;
//            [tempLabel setTitleColor:JXThemeColor forState:UIControlStateNormal];
//        }
//    }
//}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        //打开推送过来的通知
        JXAlertView *jxalertView = (JXAlertView *)alertView;
        NSDictionary *userInfo = (NSDictionary *)jxalertView.obj;
        NSString *url = [userInfo objectForKey:@"value"][0];
        //处理弹出的alertview通知
        [Utils dealUrlWithController:self.viewControllers.lastObject url:url title:@"" fromWebView:NO];
    }
}



@end
